package com.imooc.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.imooc.dto.User;
import com.imooc.dto.User.UserDetialView;
import com.imooc.dto.User.UserSimpleView;
import com.imooc.dto.UserQueryCondition;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@PostMapping
	@JsonView(UserSimpleView.class)
	public User creatUser(@Valid @RequestBody User user) {
		System.out.println(user.getId());
		System.out.println(user.getUsername());
		System.out.println(user.getPassword());
		System.out.println(user.getBirthday());
		user.setId("1");
		return user;
	}
	
	@DeleteMapping("/{id:\\d+}")
	public void deleteUser(@PathVariable String id) {
		System.out.println(id);
	}
	
	@PutMapping("/{id:\\d+}")
	public User updateUser(@Valid @RequestBody User user, BindingResult errors) {
		if(errors.hasErrors()) {
			errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
		}
		
		System.out.println(user.getId());
		System.out.println(user.getUsername());
		System.out.println(user.getPassword());
		System.out.println(user.getBirthday());
		return user;
	}
	
	@GetMapping
	@JsonView(UserSimpleView.class)
	public List<User> query(UserQueryCondition userQueryCondition, @PageableDefault(size = 10, page = 1, sort = "creatTime,asc") Pageable pageable) {
		//反射工具，将对象tostring打印到控制太上
		System.out.println(ReflectionToStringBuilder.toString(userQueryCondition, ToStringStyle.MULTI_LINE_STYLE));
		
		System.out.println(pageable.getPageSize());
		System.out.println(pageable.getPageNumber());
		System.out.println(pageable.getSort());
		
		List<User> list = new ArrayList<>();
		list.add(new User());
		list.add(new User());
		list.add(new User());
		return list;
	}

	/**
	 * url中可以带正则表达式
	 * {id:\\d+}	表示传入的id只能为数字
	 * @param id
	 * @return
	 */
	@GetMapping("/{id:\\d+}")
	@JsonView(UserDetialView.class)
	@ApiOperation(value = "用户查询详情")
	public User getInfo(@ApiParam(value = "用户ID") @PathVariable String id) {
		
		//throw new UserNotExitException(id);
		System.out.println(id);
		User user = new User("Jon", "8888");
		return user;
	}
}
